//封装axios请求
//1.导入axios
import axios from 'axios'
import router from "../router/index"

const instance = axios.create({
    // baseURL: 'http://www.wenhe.info:13784/travel'
    baseURL: 'http://localhost:8084'
})

// 请求拦截器(给后台发送的请求)
instance.interceptors.request.use(config => {
    // 获取token
    let token = sessionStorage.getItem("token");
    if (token) {
        // config.headers['Authorization'] = token
        config.headers['Authorization'] = token
    }
    return config
})

// 响应拦截器（后台返回来的参数）
instance.interceptors.response.use(
    response => {
      let rst = response.data
      if (rst && rst.code==109) {
        // 清空token
        sessionStorage.setItem("token", null)
        // 跳转登录页面
        router.push("/login")
      }

      return response.data
    },
    error => new Promise((e) => {
        // console.log()
    }) // 返回一个pedding状态的promise
)



export default instance
